import LXCOM from './common.js'

class MapViewParam {
    constructor(mapwid, maphgt, worldMapRect,editMode) {
        this.mapWindowRect = new LXCOM.LGXRect(0, maphgt, mapwid, 0);//画布对应界面尺寸。世界屏幕坐标系

        this.projectScale = 1;//投影矩阵缩放参数
        this.projOrigX = 0;//投影矩阵原点X
        this.projOrigY = 0;//投影矩阵原点Y

        //下面两个参数组成模型变换矩阵，二维进行简单处理，不使用矩阵运算
        this.scaling = 1;//视口矩阵缩放系数
        this.viewPortRect = new LXCOM.LGXRect(0, 0, 0, 0);//当前视口区域,世界坐标系。左下角为视口坐标系原点
        this.worldMapRect = new LXCOM.LGXRect(0,0,0,0);//世界最大区域，即世界坐标范围。
        this.resetWorldMap(worldMapRect);

        this._disScope = false;
        this._mapScale = 1;

        this.fillPatternCanvas = document.createElement("canvas");
        this.fillPatternCanvas.setAttribute("width", 20);
        this.fillPatternCanvas.setAttribute("height",20);
        this.fillPatternMap = this.fillPatternCanvas.getContext("2d");
        this.paintForGenerateICON = false;
        this.backColor = new LXCOM.LGXColor();
        this.backColor.setVal(255,255,255,0);
        this.editMode = editMode;
        this.showCustCursor = false;
        this.custCursorPt = new LXCOM.LGXPoint();
        this.custCursorVector = new LXCOM.LGXVector();
        this.paint4PngExport = false;
        this.clearViewPortFlag = true;

        this.showLineWidth = false;//显示线宽
        this.disgrid = false;//显示网格
        this.gridSpan = 20;//网格尺寸
        this.gridColor = new LXCOM.LGXColor();
        this.gridColor.setByRGBString("rgba(128, 128, 128, 1)");
    }

    resizeMap(mapwid,maphgt){
        this.mapWindowRect = new LXCOM.LGXRect(0, maphgt, mapwid, 0);//画布对应界面
    }

    
    resetWorldMap(worldMapRect){
        let mapwid = this.mapWindowRect.width();
        let maphgt = this.mapWindowRect.height();
        let wolrdWid = worldMapRect.right - worldMapRect.left;
        let sx = wolrdWid/mapwid;

        let wolrdHgt = worldMapRect.top - worldMapRect.bottom;
        let sy = wolrdHgt/maphgt;
        this.projectScale = Math.max(sx,sy);//投影矩阵缩放参数
        this.projOrigX = 0;//投影矩阵原点X
        this.projOrigY = 0;//投影矩阵原点Y

        //下面两个参数组成模型变换矩阵，二维进行简单处理，不使用矩阵运算
        this.scaling = 1;//模型矩阵缩放系数
        this.viewPortRect.left = worldMapRect.left;
        this.viewPortRect.top = worldMapRect.bottom + maphgt*this.projectScale;
        this.viewPortRect.right = worldMapRect.left + mapwid*this.projectScale;
        this.viewPortRect.bottom = worldMapRect.bottom;
        this.worldMapRect.clone(worldMapRect);
    }

    setScaling(s) { this.scaling = s; }
    getScaling() { return this.scaling; }
    setProjectScale(v) { this.projectScale = v; }
    getProjectScale() { return this.projectScale; }

    setDisScope(v){this._disScope = v;}
    getDisScope(){return this._disScope;}

    setMapScale(s){this._mapScale = s;}
    getMapScale(s){return this._mapScale;}
    setBackColor(v){this.backColor.clone(v);}
    getBackColor(){
        let t = new LXCOM.LGXColor();
        t.clone(this.backColor);
        return t;
    }


    getViewPort() {
        var viewRect = new LXCOM.LGXRect(0, 0, 0, 0);//当前视口区域
        viewRect.left = this.viewPortRect.left;
        viewRect.top = this.viewPortRect.top;
        viewRect.right = this.viewPortRect.right;
        viewRect.bottom = this.viewPortRect.bottom;
        return viewRect;
    }

    getWorldMapRect() {
        var rect = new LXCOM.LGXRect(0, 0, 0, 0);//世界坐标系范围
        rect.left = this.worldMapRect.left;
        rect.top = this.worldMapRect.top;
        rect.right = this.worldMapRect.right;
        rect.bottom = this.worldMapRect.bottom;
        return rect;
    }

    getMapWindowRect() {
        var rect = new LXCOM.LGXRect(0, 0, 0, 0);//屏幕坐标系范围
        rect.left = this.mapWindowRect.left;
        rect.top = this.mapWindowRect.top;
        rect.right = this.mapWindowRect.right;
        rect.bottom = this.mapWindowRect.bottom;
        return rect;
    }

    getMapWidth(){
        return this.mapWindowRect.width();
    }

    getMapHeight(){
        return this.mapWindowRect.height();
    }

    setViewPort(left, top, right, bottom) {
        this.viewPortRect.left = left;
        this.viewPortRect.top = top;
        this.viewPortRect.right = right;
        this.viewPortRect.bottom = bottom;
    }

    getMapSize(){
        return {width:this.mapWindowRect.right,height:this.mapWindowRect.top};
    }


    //长度类图形元素如距离、半径等显示时的缩放系数，直接对应到屏幕坐标
    getLenthDisScale(){
        return this.scaling/(this.projectScale);
    }

    setShowCustCursor(v){this.showCustCursor = v;}
    getShowCustCursor(){return this.showCustCursor;}

    setCustCursorPt(v){this.custCursorPt.clone(v);}
    getCustCursorPt(){return this.custCursorPt;}

    setCustCursorVector(v){this.custCursorVector.clone(v);}
    getCustCursorVector(){return this.custCursorVector;}
    setPaint4PngExport(v){this.paint4PngExport = v;}

    setDisGrid(v){this.disgrid = v;}
    getDisGrid(){return this.disgrid;}

    setClearViewPortFlag(v){this.clearViewPortFlag = v;}
    getClearViewPortFlag(){return this.clearViewPortFlag;}

    setLineWidthShow(v){this.showLineWidth = v;}
    getLineWidthShow(){return this.showLineWidth;}

    setGridSpan(v){this.gridSpan = v;}
    getGridSpan(){return this.gridSpan;}

    setGridColor(v){this.gridColor = v;}
    getGridColor(){return this.gridColor;}
}

export default { MapViewParam }